﻿//---------------------------------------------------------------------------
// <copyright file="RibbonHighlightingListItem.cs" company="Microsoft Corporation">
//     Copyright (C) Microsoft Corporation.  All rights reserved.
// </copyright>
//---------------------------------------------------------------------------

namespace Microsoft.Windows.Controls.Ribbon
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    
    /// <summary>
    ///   An item for a RibbonHighlightingList which raises calls back the list upon selection.
    /// </summary>
    public class RibbonHighlightingListItem : ContentControl
    {
        /// <summary>
        ///   Initializes static members of the RibbonHighlightingListItem class.  This also overrides the
        ///   default style and adds a class handler to watch for MouseLeftButtonUp.
        /// </summary>
        static RibbonHighlightingListItem()
        {
            Type ownerType = typeof(RibbonHighlightingListItem);

            DefaultStyleKeyProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ownerType));
            EventManager.RegisterClassHandler(ownerType, UIElement.MouseLeftButtonUpEvent, new MouseButtonEventHandler(OnMouseLeftButtonUp));
        }

        /// <summary>
        ///   Called on left button up.  Notifies the containing RibbonHighlightingList
        ///   that this item was selected.
        /// </summary>
        /// <param name="sender">The item that was selected.</param>
        /// <param name="e">The event data.</param>
        private static void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            RibbonHighlightingListItem ribbonMostRecentFileItem = (RibbonHighlightingListItem)sender;
            RibbonHighlightingList ribbonMostRecentFileItemsControl = ItemsControl.ItemsControlFromItemContainer(ribbonMostRecentFileItem) as RibbonHighlightingList;

            if (ribbonMostRecentFileItemsControl != null)
            {
                ribbonMostRecentFileItemsControl.OnMostRecentFileSelectedInternal(ribbonMostRecentFileItem.Content);
            }
        }
    }
}